package com.vay.de.dang.online.ui.apply;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.vay.de.dang.online.VnApp;
import com.vay.de.dang.online.util.VnUtil;
import com.vay.de.dang.online.web.BaseApi;
import com.vay.de.dang.online.web.LoanApi;
import com.vay.de.dang.online.web.ReqResult;
import com.vay.de.dang.online.R;
import com.vay.de.dang.online.databinding.AcRecommendBinding;
import com.vay.de.dang.online.entiy.CommodityEntity;
import com.vay.de.dang.online.ui.WithBack1Activity;
import com.vay.de.dang.online.ui.HomeActivity;
import com.vay.de.dang.online.util.VnHandler;
import com.vay.de.dang.online.web.ReqCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * ProjectName: NsVnm
 * Author     : 南山
 * Date       : 2023/12/3
 * Describe   : 类描述
 */
public class RecommendActivity extends WithBack1Activity {

    // 全局数据
    public static List<CommodityEntity> allData;

    private AcRecommendBinding binding;

    private List<ImageView> cbList = new ArrayList<>();
    private List<String> applyNo = new ArrayList<>();
    private boolean selectAll;
    private List<CommodityEntity> allProduct = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = AcRecommendBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        ((TextView) findViewById(R.id.title_tv)).setText(R.string.title_recommend);
        findViewById(R.id.bnt_back_home).setOnClickListener(view -> {
            finish();
            // 业务埋点
            BaseApi.execBuriedPoint(13010, 13000, "商品推荐页面", "推荐页返回");
        });

        // 业务埋点
        BaseApi.execBuriedPoint(13000, 13000, "商品推荐页面", "推荐页可见");

        binding.bntFull.setOnClickListener(view -> {
            selectAll = !selectAll;
            checkNo(selectAll ? "all" : "none");
        });

        binding.bntRefresh.setOnClickListener(view -> {
            refreshData();
        });

        binding.bntApply.setOnClickListener(view -> {

            if (applyNo.isEmpty()) {
                showHint(getString(R.string.label_on_click_apply_empty));
                return;
            }

            StringBuilder str = new StringBuilder();
            for (String s : applyNo) {
                if (str.length() > 0) str.append(",");
                str.append(s);
            }

            // 业务埋点
            BaseApi.execBuriedPoint(13000, 13020, "申请", str.toString());

            LoanApi.onClickApply(applyNo, new ReqCallback(mActivity) {
                @Override
                public void callback(ReqResult value) {
                    if (value.isOk()) {
                        HomeActivity.reloadHome();
                        VnHandler.showText(getString(R.string.label_on_click_apply_ok));
                        finish();
                    }
                }
            }.showDialog(true));
        });

        loadDataAndShow(allData);
    }

    @Override
    public void onBackPressed() {

        // 业务埋点
        BaseApi.execBuriedPoint(13010, 13000,
                "商品推荐页面", "推荐页返回");

        super.onBackPressed();
    }

    private void refreshData() {
        LoanApi.queryRecommend(new ReqCallback(mActivity) {
            @Override
            public void callback(ReqResult value) {
                if (value.isOk()) {
                    loadDataAndShow(allData = (List<CommodityEntity>) value.result);
                }
            }
        }.showDialog(true));
    }

    private void loadDataAndShow(List<CommodityEntity> list) {

        if (VnUtil.isEmpty(list)) {
            VnHandler.showText(VnApp.getStr(R.string.label_no_recommend));
            finish();
            return;
        }

        binding.itemLayout.removeAllViews();
        cbList.clear();
        applyNo.clear();
        allProduct.clear();
        allProduct.addAll(list);
        for (int index = 0; index < list.size(); index++) {

            CommodityEntity entity = list.get(index);
            View iv = getLayoutInflater().inflate(R.layout.item_recommend_product, null);
            TextView pName = iv.findViewById(R.id.tv_name);
            TextView pMoney = iv.findViewById(R.id.tv_money);
            //TextView fastLending = iv.findViewById(R.id.fast_lending_tv);
            //TextView dailyRate = iv.findViewById(R.id.daily_rate_tv);
            TextView periods = iv.findViewById(R.id.tv_periods);
            ImageView cb = iv.findViewById(R.id.cb);
            cb.setTag(entity);
            cb.setOnClickListener(v -> {
                checkNo(entity.getCommodityNum());
            });
            cbList.add(cb);

            applyNo.add(entity.getCommodityNum());

            pName.setText(entity.getCommodityName());
            pMoney.setText(VnUtil.toMoneyStr2(entity.getMinQuota()) + "-" + VnUtil.toMoneyStr2(entity.getMaxQuota()));
            //fastLending.setText(entity.getFastestLoan() + " min");
            //dailyRate.setText(entity.getDayRate() + "%");
            periods.setText(entity.getMinLoanTerm() + "-" + entity.getMaxLoanTerm() + "Days");

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            lp.topMargin = index > 0 ? VnApp.getDimension(R.dimen.x12) : 0;
            lp.leftMargin = lp.rightMargin = VnApp.getDimension(R.dimen.x16);

            binding.itemLayout.addView(iv, lp);
        }

        binding.bntFull.setVisibility(list.size() > 1 ? View.VISIBLE : View.INVISIBLE);
    }

    private void checkNo(String no) {
        if ("all".equals(no)) {
            applyNo.clear();
            for (CommodityEntity entity : allProduct) {
                applyNo.add(entity.getCommodityNum());
            }
        } else if ("none".equals(no)) {
            applyNo.clear();
        } else {
            if (applyNo.contains(no)) {
                applyNo.clear();
            } else {
                applyNo.add(no);
            }
        }

        for (ImageView cb : cbList) {
            CommodityEntity entity = (CommodityEntity) cb.getTag();
            boolean selected = applyNo.contains(entity.getCommodityNum());
            cb.setImageResource(selected ? R.mipmap.ic_cb_pre : R.mipmap.ic_cb_nor);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        allData = null;
    }
}
